package com.haierCamera.customapplication.ui.main;

import android.Manifest;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.ApiService;
import com.haierCamera.customapplication.databinding.HzklActivityDeviceDetailBinding;
import com.haierCamera.customapplication.ui.business.Business;
import com.haierCamera.customapplication.ui.main.widget.DeleteDialog;
import com.haierCamera.customapplication.ui.plan.HZKLDeviceMoreActivity;
import com.haierCamera.customapplication.ui.plan.HZKLDeviceSetActivity;
import com.haierCamera.customapplication.utils.ResourceConvertUtils;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import dagger.Lazy;

public class HZKLCameraDeviceDetailActivity extends BaseActivity {
    HzklActivityDeviceDetailBinding binding;

    @Inject
    Lazy<HttpErrorProcess> httpErrorProcessLazy;
    @Inject
    ApiService apiService;

    String old_V = "";
    String new_V = "";
    boolean canUpgrade = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_device_detail);

        binding.imgReturn.setOnClickListener((v)->{
            finish();
        });

        binding.layout1.setOnClickListener((v)->{
            startActivity(new Intent(HZKLCameraDeviceDetailActivity.this
                    ,HZKLCameraDeviceEditActivity.class)
                    .putExtra("sn",getIntent().getStringExtra("sn"))
                    .putExtra("accountDeviceId",getIntent().getStringExtra("accountDeviceId"))
                    .putExtra("name",getIntent().getStringExtra("name"))
                    .putExtra("sc",getIntent().getStringExtra("sc"))
                    .putExtra("picture",getIntent().getStringExtra("picture")));
        });

        binding.layout2.setOnClickListener((v)->{
            if ("".equals(old_V)){
                toast("未获取更新信息");
                return;
            }
            if (!canUpgrade) {
                toast("已是更新版本");
                return;
            }

            startActivityForResult(new Intent(HZKLCameraDeviceDetailActivity.this
                    ,HZKLCameraDeviceVersionActivity.class)
                    .putExtra("devId",getIntent().getStringExtra("sn"))
                    .putExtra("old_V",old_V)
                    .putExtra("new_V",new_V),10000);
        });

        binding.btnDel.setOnClickListener((v)->{

            DeleteDialog customDialog = new DeleteDialog(HZKLCameraDeviceDetailActivity.this);
            customDialog.setTitle("确认删除");
            customDialog.setCancel("取消", new DeleteDialog.IOnCancelListener() {
                @Override
                public void onCancel(DeleteDialog dialog) {
                    dialog.dismiss();
                }
            });
            customDialog.setConfirm("确认", new DeleteDialog.IOnConfirmListener() {
                @Override
                public void onConfirm(DeleteDialog dialog) {
                    unBindDevice(Long.parseLong(getIntent().getStringExtra("accountDeviceId")));
                    dialog.dismiss();
                }
            });
            customDialog.show();

        });

        binding.layoutNotify.setOnClickListener((v)->{
            startActivity(new Intent(this
                    ,HZKLMsgAlarmSettingActivity.class)
                    .putExtra("devId","" + getIntent().getStringExtra("sn")));
        });

        binding.layoutDefence.setOnClickListener((v)->{
            startActivity(new Intent(this, HZKLDeviceSetActivity.class)
                    .putExtra("ability",getIntent().getStringExtra("ability"))
                    .putExtra("deviceCode", "" + getIntent().getStringExtra("sn")));
        });
        binding.layoutMore.setOnClickListener((v)->{
            startActivity(new Intent(this, HZKLDeviceMoreActivity.class)
                    .putExtra("deviceCode", "" + getIntent().getStringExtra("sn")));
        });
        init();
    }

    private void init(){
        binding.tvDevice.setText(getIntent().getStringExtra("name"));
        if (getIntent().getStringExtra("picture")!=null
                && !getIntent().getStringExtra("picture").equals("")) {
//            Log.i("TAG","init_picture-" + getIntent().getStringExtra("picture"));
            RequestOptions coverRequestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                    .skipMemoryCache(true);//不做内存缓存

            Glide.with(this)
                    .load(getIntent().getStringExtra("picture"))
                    .apply(coverRequestOptions)
                    .into(binding.ivDeviceImg);
        }

        Business.getInstance().getDeviceUpgradeInfo2(getIntent().getStringExtra("sn"),new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if(msg.what == 0) {
                    Bundle b = (Bundle) msg.obj;
                    if (b.getBoolean("canBeUpgrade")) {
                        binding.ivNotifyUpgrade.setVisibility(View.VISIBLE);
                    } else {
                        binding.ivNotifyUpgrade.setVisibility(View.GONE);
                    }

                    canUpgrade = b.getBoolean("canBeUpgrade");
                    old_V = b.getString("version");
                    if (b.getBoolean("canBeUpgrade")) {
                        new_V = b.getString("upgradeVersion");
                    }
                    binding.tvSysVersion.setText(b.getString("version"));
                }
            }
        });

    }

    public void unBindDevice(long accountDeviceId) {
        ResourceConvertUtils.convertToResource(apiService.unbind(accountDeviceId))
                .observe(this, resource -> {
                    assert resource != null;
                    switch (resource.status) {
                        case ERROR:
                            httpErrorProcessLazy.get().process(resource);

                            break;
                        case SUCCESS:
                            Intent intent = new Intent();
                            setResult(RESULT_OK,intent);
                            finish();

                            break;
                        case LOADING:
                            break;

                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 10000) {
                binding.ivNotifyUpgrade.setVisibility(View.GONE);

            }
        }
    }
}
